<?php
require_once('class.restrequest.php');
class Jira {
	protected $project;
	protected $host;
	function __construct($config){
		$this->request = new RestRequest;
		$this->request->username = $config->username;
		$this->request->password = $config->password;
		$this->host = $config->host;
	}
	public function createIssue($json){
		$this->request->openConnect('https://'.$this->host.'/rest/api/latest/issue/', 'POST', $json);
		$this->request->execute(); 
		return $this->request;		
	}
    public function addAttachment($filename, $issueKey){
		$this->request->openConnect('https://'.$this->host.'/rest/api/latest/issue/'.$issueKey.'/attachments', 'POST', null, $filename);
		$this->request->execute(); 
		return $this->request;
    }
	public function updateIssue($json, $issueKey){
		$this->request->openConnect('https://'.$this->host.'/rest/api/latest/issue/'.$issueKey, 'PUT', $json);
		$this->request->execute();  
	}
	public function queryIssue($query){
		$qs = $this->createPairs($query);
                //var_dump($qs); exit;
		$this->request->OpenConnect('https://'.$this->host.'/rest/api/latest/search?jql='.$qs);
		$this->request->execute(); 
		return $this->request;
	}
	public function queryMoreIssue($query){
		$this->request->OpenConnect('https://'.$this->host.'/rest/api/latest/search?jql=', 'POST', $query);
		$this->request->execute();  
		return $this->request;
	}
	public function updateTransition($issueKey,$json){
		$this->request->openConnect('https://'.$this->host.'/rest/api/latest/issue/'.$issueKey.'/transitions?', 'POST', $json);
		$this->request->execute();
		return $this->request;
	}
	function createPairs($obj){
			$str = "";
			foreach ($obj as $key => $value) {
    			$str .= "$key=$value&";
			}
			return rtrim($str, '&');
		}
	public function queryUser($query){
		
		function createPairs($obj){
			$str = "";
			foreach ($obj as $key => $value) {
    			$str .= "$key=$value&";
			}
			return rtrim($str, '&');
		}
		$qs = createPairs($query);
		$this->request->OpenConnect('https://'.$this->host.'/rest/api/latest/user/assignable/search?'.$qs);
		$this->request->execute();  
		return $this->request;
	}
        public function getUser($user){
            $this->request->openConnect('https://'.$this->host.'/rest/api/2/user?username='.$user);
            $this->request->execute();  
            return $this->request;
        }
        public function queryStatus($issueKey){
           // var_dump($issueKey);
		$this->request->openConnect('https://'.$this->host.'/rest/api/2/issue/'.$issueKey.'/transitions?');
		$this->request->execute();
		return $this->request;
	}
	public function queryAllStatus(){
		$this->request->openConnect('https://'.$this->host.'/rest/api/latest/status');
		$this->request->execute();
		return $this->request;
	}
	public function queryAllPriority(){
		$this->request->openConnect('https://'.$this->host.'/rest/api/latest/priority');
		$this->request->execute();
		return $this->request;
	}
	public function getStatusbyId($jiraid){
		$this->request->openConnect('https://'.$this->host.'/rest/api/latest/status/'.$jiraid);
		$this->request->execute(); 
		return $this->request;
	}
	public function queryChangelog($issueKey){
		$this->request->openConnect('https://'.$this->host.'/rest/api/latest/issue/'.$issueKey.'?expand=changelog');
		$this->request->execute();
		return $this->request;
	}
	public function deleteIssue($issueKey){
		$un=$this->request->openConnect('https://'.$this->host.'/rest/api/latest/issue/'.$issueKey, 'DELETE');
		$this->request->execute();  
		return $this->request;
    }
	public function queryUserGroup($username){
		$this->request->OpenConnect('https://'.$this->host.'/rest/api/latest/user?username='.$username.'&expand=groups');
		$this->request->execute();
		return $this->request;
	}
	public function queryComment($issueKey){
		$this->request->openConnect('https://'.$this->host.'/rest/api/latest/issue/'.$issueKey.'/comment');
		$this->request->execute();
		return $this->request;
	}
	public function updateComment($issueKey,$commentID,$json){
		$this->request->openConnect('https://'.$this->host.'/rest/api/latest/issue/'.$issueKey.'/comment/'.$commentID,'PUT',$json);
		$this->request->execute();
		return $this->request;
	}
	public function deleteComment($issueKey,$commentID){
		$this->request->openConnect('https://'.$this->host.'/rest/api/latest/issue/'.$issueKey.'/comment/'.$commentID,'DELETE');
		$this->request->execute();
		return $this->request;
	}
	public function addComment($issueKey,$json){
		$this->request->openConnect('https://'.$this->host.'/rest/api/latest/issue/'.$issueKey.'/comment/','POST',$json);
		$this->request->execute();
		return $this->request;
	}
	public function queryPermission($project){
		$this->request->OpenConnect('https://'.$this->host.'/rest/api/latest/mypermissions?projectKey='.$project);
		$this->request->execute();
		return $this->request;
	}
	
	public function Test(){
		$this->request->OpenConnect('https://'.$this->host.'/rest/api/latest/projects');
		$this->request->execute();
		//print_r($this->request);
		return $this->request;
	}
}
?>
